package edu.gatech.TNE.wonderLab7000.M9;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import oracle.jdbc.pool.OracleDataSource;
/**
 * Team Captain Class
 * 
 * @author Chad
 *
 */
public class TeamCaptain {
	
	private PreparedStatement statement;
    private Connection conn;
    private ResultSet result;
    private String query, captainName, league;
    
	public TeamCaptain (String captainName, String league){
    	
    	this.captainName = captainName;
    	this.league = league;
    	
    }
	
	/**
	 * Captain Name Getter
	 * 
	 * @return Captain's Name
	 */
    public String getCaptainName() {
		return captainName;
	}
    
    /**
     * League Getter
     * 
     * @return League Name
     */
	public String getLeague() {
		return league;
	}
	
	/**
	 * 
	 * League Setter
	 * 
	 * @param league
	 */
	public void setLeague(String league) {
		this.league = league;
	}

	/**
	 * Captain Name Setter
	 * 
	 * @param captainName
	 */
	public void setCaptainName(String captainName) {
		this.captainName = captainName;
	}
    
    /**
     * Database Connector
     * 
     * @return Connection
     * @throws SQLException
     */
	public Connection connect() throws SQLException {
        OracleDataSource ods = new OracleDataSource();
        ods.setURL("jdbc:oracle:thin:ops$mdye3/wonderLab@acme.gatech.edu:1525:public");
        conn = ods.getConnection();
        return conn;
    }
	
	/**
	 * Creates a team in a league
	 * 
	 * @param teamName
	 * @param capacity
	 * @return true if team is created, false if not
	 * @throws SQLException
	 */
	public boolean createTeam (String teamName, int capacity) throws SQLException {
		
		connect();
		query = "SELECT * FROM " + league + " WHERE TEAMNAME ='" + teamName + "'";
	    statement = conn.prepareStatement(query);
	    try {
	    	result = statement.executeQuery();
		} catch (SQLException e) {
	        conn.close();
	        statement.close();
			System.out.println(e.toString());
		} 
		
		if (result.next()) {
	        conn.close();
	        statement.close();
	        result.close();
			return false; 
		}
		
		query = "INSERT INTO " + league + "(TEAMNAME, CAPACITY, CAPTAIN) VALUES" +
				"('" + teamName + "', " + capacity + ", '" + captainName + "')";
	    statement = conn.prepareStatement(query);
	    try {
	    	result = statement.executeQuery();
		} catch (SQLException e) {
	        conn.close();
	        statement.close();
	        result.close();
			System.out.println(e.toString());
		} 
        conn.close();
        statement.close();
        result.close();
		return true;
		
	}
	
	public boolean deleteTeam (String teamName) throws SQLException {
		
		connect();
		query = "SELECT * FROM " + league + " WHERE TEAMNAME ='" + teamName + "'";
	    statement = conn.prepareStatement(query);
	    try {
	    	result = statement.executeQuery();
		} catch (SQLException e) {
	        conn.close();
	        statement.close();
	        result.close();
			System.out.println(e.toString());
		} 
		
		if (!result.next()) {
	        conn.close();
	        statement.close();
	        result.close();
			return false; 
		}
		
		query = "DELETE FROM " + league + " WHERE TEAMNAME ='" + teamName + "'";
	    statement = conn.prepareStatement(query);
	    try {
	    	result = statement.executeQuery();
		} catch (SQLException e) {
	        conn.close();
	        statement.close();
	        result.close();
			System.out.println(e.toString());
		} 
		return true;
		
	}
	
}
